<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RegisterForm
 *
 * @author hp
 */
class SocialMediaSelectorForm extends sfForm{

    /**
     * Form Configuration
     * Embed form and model relations
     */
    public function configure(){
        
        $social_media_defaults = sfConfig::get('app_social_media_defaults');
        $i = 0;
        foreach($social_media_defaults as $social_media_id => $social_media_label){
            $social_media_form = new UserSocialMediaForm();
            $social_media_form->setDefault('social_media_id', $social_media_id);
            $social_media_form->offsetUnset('user_id');
            $social_media_form->getObject()->setUser($this->getOption('user'));
            $form_name = $social_media_form->getName().'_'.$i;
            $this->embedForm($form_name, $social_media_form);
            $i++;
        }        
    }

    public function getName(){
        return $this->getOption('name');
    }

    public function setUser(User $user){
        $this->user = $user;
    }

    /**
     * Manually saves embeded form's objects
     */
    public function save(User $user){
        if($this->isBound()){
            foreach($this->getEmbeddedForms() as $form){
                $object = $form->getObject();
                $object->fromArray($this->values[$form->getName()]);
                $object->setUser($this->user);
                $object->save();
            }
        } else {
            throw new Exception('Form is not valid!');
        }
    }

}
?>
